package org.weasel.controllers;

import java.io.File;
import java.sql.Time;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Composite;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;
import org.weasel.business.UserStory;
import org.weasel.views.IterationChartComposite;

/**
 * This class is the controller of the DeveloperComposite.java which represents a composite
 * to move users from one list to another. Depending to which controller this class here is attached, the moving
 * will have effect between company<->project or between project<->userstory developers.
 *
 */
public class IterationChartController {
	@SuppressWarnings("unused")
	private IterationViewController parentController;
	private IterationChartComposite iterationChartComposite; //holding the composite with the two dev lists

	private ArrayList<UserStory> userStories;

	public IterationChartController(){
		
	}
	
	/**
	 * If attaching it to the projectDetailView
	 * @param parentController
	 */
	public void setProjectViewController(IterationViewController parentController){
		this.parentController = parentController;
	}
	
	
	public void refreshComposite(){
		writeChartToFile();
		iterationChartComposite.reload();
	}
	
	public void loadUserStories(ArrayList<UserStory> userStories){
		this.userStories = userStories;
	}
	
	/**
	 * Both parameters are passed by the viewpart (over the controller)
	 * where the composite should be displayed
	 * @param parent
	 * @param style
	 */
	public IterationChartComposite loadIterationChartComposite(Composite parent, int style){
		iterationChartComposite = new IterationChartComposite(parent,style,userStories, this);
		return iterationChartComposite;
	}
	
	public void writeChartToFile(){
		JFreeChart chart = createChart(); //make the chart
		try{
			File f=new File("chart.jpg");
			ChartUtilities.saveChartAsJPEG(f, chart, 500, 300);
		}catch (java.io.IOException ex){
			ex.printStackTrace();
		}
	}
	
	private JFreeChart createChart() {
		final PieDataset dataset = createDataset();
		final JFreeChart chart = ChartFactory.createPieChart3D(
				"Iteration completion chart",  // chart title
				dataset,                // data
				true,                   // include legend
				true,
				false
		);

//		BarRenderer r = new BarRenderer();
		final PiePlot3D plot = (PiePlot3D) chart.getPlot();
		plot.setStartAngle(290);
		plot.setDirection(Rotation.CLOCKWISE);
		plot.setForegroundAlpha(0.5f);
		plot.setNoDataMessage("No data to display");
		plot.setLabelGenerator(null);
		return chart;
	}
	
	private PieDataset createDataset() { 
		final DefaultPieDataset result = new DefaultPieDataset();
		int undone= 0;
		for (UserStory userStory : userStories) {
			Time usedTime = userStory.getUsedTime();
			if (usedTime.toString().equals("00:00:00"))
				undone++;
		}
		int storyCount = userStories.size();
		String tmpUndone = String.valueOf(undone);
		String tmpStoryCount = String.valueOf(storyCount);
		
		double undonePercent = Double.parseDouble(tmpUndone) / Double.parseDouble(tmpStoryCount);
		double donePercent = 1 - undonePercent;
		result.setValue("Done", donePercent);
		result.setValue("To do", undonePercent);
		return result;   
	}
	
}
